package com.wxg.rocket;

import com.wxg.config.rocketmq.IRocketMqSelector;
import com.wxg.domain.MqConstant;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.junit.Test;

import java.util.List;

/**
 * @author void
 * @date 2019/12/11 17:27
 * @desc
 */
public class SyncProducer {

    @Test
    public void test1() throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer producer = new DefaultMQProducer("GID_VOID_1");
        producer.setNamesrvAddr("localhost:9876");
        producer.start();
        for (int i=0;i<100;i++){
            Message msg = new Message("TOPIC_VOID_1", "TagA", "hello rocketMq".getBytes());
            //1s、 5s、 10s、 30s、 1m、 2m、 3m、 4m、 5m、 6m、 7m、 8m、 9m、 10m、 20m、 30m、 1h、 2h
            //延迟消息 18个level
            msg.setDelayTimeLevel(3);
            SendResult sendResult = producer.send(msg);
            System.out.println(sendResult);
        }
        producer.shutdown();
    }
    
    @Test
    public void test2() throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer producer = new DefaultMQProducer("GID_VOID_2");
        producer.setNamesrvAddr("172.16.1.100:9876");
        producer.start();
        Message msg = new Message("TOPIC_VOID_2", "TagB", "hello rocketMq 测试渠道".getBytes());
        SendResult sendResult = producer.send(msg, new IRocketMqSelector(), MqConstant.env);
        System.out.println(sendResult);
        producer.shutdown();
    }
}
